using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using NUnit.Framework;

namespace Pinky.Xml.Tests
{
    [TestFixture]
    public class SerializerTests
    {
        private const string SimpleEntityAsXml = @"<?xml version=""1.0"" encoding=""utf-16""?><SimpleEntity><Id>1</Id><Name>Jack</Name><Something><Dupa>Jasne</Dupa><Id>666</Id></Something></SimpleEntity>";

        [Test]
        public void Write_WhenObjectHasData_ThenXmlHasDataToo()
        {
            var se = new SimpleEntity()
                         {
                             Id = 1,
                             Name = "Jack",
                             Something = new Component
                                             {
                                                 Dupa = "Jasne",
                                                 Id = 666
                                             }
                         };
            var s = new XmlSerializer();
            var sw = new StringWriter();
            
            using (XmlWriter xmlWriter = XmlWriter.Create(sw))
            {
                s.Write(xmlWriter, se, "SimpleEntity");
            }

            var xml = sw.ToString();
            Assert.AreEqual(
                Regex.Replace(SimpleEntityAsXml, @"\s+", string.Empty),
                Regex.Replace(xml, @"\s+", string.Empty));
        }

        [Test]
        public void Read_WhenXmlHasData_ThenObjectWillHaveDataToo()
        {
            var s = new XmlSerializer();
            SimpleEntity o;
            using (var xmlTextReader = new XmlTextReader(new StringReader(SimpleEntityAsXml)))
            {
                o = s.Read<SimpleEntity>(xmlTextReader);
            }

            Assert.AreEqual(1, o.Id);
            Assert.AreEqual("Jack", o.Name);
            Assert.IsNotNull(o.Something);
            Assert.AreEqual(666, o.Something.Id);
            Assert.AreEqual("Jasne", o.Something.Dupa);
        }
    }
}